package uk.dier.account.managed;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import uk.dier.account.dto.MarketDto;
import uk.dier.account.entity.Market;
import uk.dier.account.exception.ExceptionDao;
import uk.dier.account.exception.ExceptionService;
import uk.dier.account.service.ISMarketService;

@ManagedBean
@Controller
@RequestScoped
public class UserManagedBean implements Serializable {
	
	private static final long serialVersionUID = -63294555240167124L;
	private static final Logger logger = LoggerFactory.getLogger(UserManagedBean.class);
	private static final String SUCCESS = "success";
	private static final String ERROR   = "error";
	
	private String name ="";
	private List<MarketDto> listOfMarket;
	
	@Autowired
	private ISMarketService marketService;
	
	public String hello() {
		try {
			listOfMarket = marketService.findMarketByName(new Market(name));
			if (listOfMarket.isEmpty()) {
				return ERROR;
			}
		} catch (ExceptionService e) {
		} catch (ExceptionDao e) {
		}
		return SUCCESS;
	}
	
	public String save() {
		if (StringUtils.isEmpty(name)) {
			return ERROR;
		} else {
			try {
				Market market = new Market(name);
				marketService.saveMarket(market);
			} catch (ExceptionService e) {
			} catch (ExceptionDao e) {
			}
			return SUCCESS;
		}
	}
	
	public void reset() {
		setName("");
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<MarketDto> getListOfMarket() {
		return listOfMarket;
	}

	public void setListOfMarket(List<MarketDto> listOfMarket) {
		this.listOfMarket = listOfMarket;
	}

	public ISMarketService getMarketService() {
		return marketService;
	}

	public void setMarketService(ISMarketService marketService) {
		this.marketService = marketService;
	}
	
	
}